app.controller("z-approvalRule-c", [ '$scope','$rootScope','$z-http','$sce','z-approvalRule-s','z-attendance-s','z-edit-menu-s','z-employee-s','z-corporation-s','z-salaryApprovalRule-s','z-department-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,AttendanceService,MenuService,EmployeeService, CorporationService,SalaryApprovalRuleService,DepartmentService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";		
		$scope.viewModel.requestType="";
		$scope.viewModel.requestRole="";

		//编辑
		$scope.editModel = {};
		$scope.editModel.approvalRule={};
		$scope.editModel.title="";
		$scope.editModel.requestTypeData=[];
		$scope.editModel.requestRoleData=[];
		$scope.editModel.oneApprovalRoleData=[];
		$scope.editModel.twoApprovalRoleData=[];
		$scope.editModel.threeApprovalData=[];
		$scope.editModel.fourApprovalRoleData=[];
		$scope.editModel.fiveApprovalRoleData=[];
		$scope.editModel.lowestApprovalRole=[];
		$scope.editModel.approvalRule.oneApprovalRole="";
		$scope.editModel.approvalRule.twoApprovalRole="";
		$scope.editModel.approvalRule.threeApprovalRole="";
		$scope.editModel.approvalRule.fourApprovalRole="";
		$scope.editModel.approvalRule.fiveApprovalRole="";

		$scope.editModel2= {};
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'approvalRule'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//选中事件
		$scope.editModel.selectBindData=function(value,sep){
			if(sep==1){
				$scope.editModel.oneApprovalRoleData="";
				AttendanceService.getDictionaryListBydicCls({
					dicCls:"9",
					dicCd:value,
				},
				function(data){
					$scope.editModel.oneApprovalRoleData= data;
					if($scope.editModel.type){
						$scope.editModel.approvalRule.oneApprovalRole="";
						$scope.editModel.approvalRule.twoApprovalRole="";
						$scope.editModel.approvalRule.threeApprovalRole="";
						$scope.editModel.approvalRule.fourApprovalRole="";
						$scope.editModel.approvalRule.fiveApprovalRole="";
						$scope.editModel.twoApprovalRoleData=[];
						$scope.editModel.threeApprovalData=[];
						$scope.editModel.fourApprovalRoleData=[];
						$scope.editModel.fiveApprovalRoleData=[];
					}
				},function(error) {
							
				});
			}else if(sep==2){
				AttendanceService.getDictionaryListBydicCls({
					dicCls:"9",
					dicCd:value
				},
				function(data){
					$scope.editModel.twoApprovalRoleData= data;
					if($scope.editModel.type){
						$scope.editModel.approvalRule.threeApprovalRole="";
						$scope.editModel.threeApprovalData=[];
						$scope.editModel.approvalRule.fourApprovalRole="";
						$scope.editModel.fourApprovalRoleData=[];
						$scope.editModel.approvalRule.fiveApprovalRole="";
						$scope.editModel.fiveApprovalRoleData=[];
					}
				},function(error) {
							
				});
			}else if(sep==3){
				AttendanceService.getDictionaryListBydicCls({
					dicCls:"9",
					dicCd:value,
				},
				function(data){
					$scope.editModel.threeApprovalData= data;
					if($scope.editModel.type){
						$scope.editModel.approvalRule.fourApprovalRole="";
						$scope.editModel.fourApprovalRoleData=[];
						$scope.editModel.approvalRule.fiveApprovalRole="";
						$scope.editModel.fiveApprovalRoleData=[];
					}
				},function(error) {
							
				});
			}else if(sep==4){
				AttendanceService.getDictionaryListBydicCls({
					dicCls:"9",
					dicCd:value
				},
				function(data){
					$scope.editModel.fourApprovalRoleData= data;
					if($scope.editModel.type){
						$scope.editModel.approvalRule.fiveApprovalRole="";
						$scope.editModel.fiveApprovalRoleData=[];
					}
				},function(error) {
							
				});
			}else if(sep==5){
				AttendanceService.getDictionaryListBydicCls({
					dicCls:"9",
					dicCd:value
				},
				function(data){
					$scope.editModel.fiveApprovalRoleData= data;
				},function(error) {
							
				});
			}
		};
		
		//加载考勤状态数据
		$scope.viewModel.bindSelectData = function(){
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"8"
			},
			function(data){
				$scope.viewModel.requestTypeData = data;
				$scope.editModel.requestTypeData = data;
			},function(error) {
						
			});
			
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"9"
			},
			function(data){
				$scope.viewModel.requestRoleData= data;
				$scope.editModel.requestRoleData= data;
				for(var i=0;i<data.length;i++){
					if(data[i].dicCd!=99){
						$scope.editModel.lowestApprovalRole.push(data[i]);
					}
				}
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
			$scope.editModel.approvalRule={};
		};
		
		//新增
		$scope.viewModel.add = function(approval){
			$scope.editModel.title="新增审批规则";
			$scope.editModel.type = true;
			$scope.editModel.approvalRule={};
			$scope.editModel.oneApprovalRoleData=[];
			$scope.editModel.twoApprovalRoleData=[];
			$scope.editModel.threeApprovalData=[];
			$scope.editModel.fourApprovalRoleData=[];
			$scope.editModel.fiveApprovalRoleData=[];
			$scope.editModel.approvalRule.twoApprovalRole="";
			$scope.editModel.approvalRule.threeApprovalRole="";
			$scope.editModel.approvalRule.fourApprovalRole="";
			$scope.editModel.approvalRule.fiveApprovalRole="";
			//验证不能为空
			$scope.editForm.requestType.$dirty=false;
			$scope.editForm.requestRole.$dirty=false;
			$scope.editForm.oneApprovalRole.$dirty=false;
			$scope.editForm.lowestApprovalRole.$dirty=false;
			$scope.viewModel.load();
		};
		
		//修正
		$scope.viewModel.update=function(approvalRule){
			$scope.editModel.title="修改审批规则";
			$scope.editModel.type = false;
			$scope.editModel.oneApprovalRoleData=[];
			$scope.editModel.twoApprovalRoleData=[];
			$scope.editModel.threeApprovalData=[];
			$scope.editModel.fourApprovalRoleData=[];
			$scope.editModel.fiveApprovalRoleData=[];
			$scope.editModel.approvalRule=angular.copy(approvalRule);
			if($scope.editModel.approvalRule.twoApprovalRole==null){
				$scope.editModel.approvalRule.twoApprovalRole="";
			}
			if($scope.editModel.approvalRule.threeApprovalRole==null){
				$scope.editModel.approvalRule.threeApprovalRole="";
			}
			if($scope.editModel.approvalRule.fourApprovalRole==null){
				$scope.editModel.approvalRule.fourApprovalRole="";
			}
			if($scope.editModel.approvalRule.fiveApprovalRole==null){
				$scope.editModel.approvalRule.fiveApprovalRole="";
			}
			$scope.editModel.selectBindData($scope.editModel.approvalRule.requestRole,1);
			$scope.editModel.selectBindData($scope.editModel.approvalRule.oneApprovalRole,2);
			$scope.editModel.selectBindData($scope.editModel.approvalRule.twoApprovalRole,3);
			$scope.editModel.selectBindData($scope.editModel.approvalRule.threeApprovalRole,4);
			$scope.editModel.selectBindData($scope.editModel.approvalRule.fourApprovalRole,5);
			$scope.viewModel.load();
		};
		
		//是否选中
		$scope.viewModel.selected=function(valeu,sep){
			if(!$scope.editModel.type){
				if($scope.editModel.approvalRule.oneApprovalRole==valeu && sep==1){
					return true;
				}
				if($scope.editModel.approvalRule.twoApprovalRole==valeu && sep==2){
					return true;
				}
				if($scope.editModel.approvalRule.threeApprovalRole==valeu && sep==3){
					return true;
				}
				if($scope.editModel.approvalRule.fourApprovalRole==valeu && sep==4){
					return true;
				}
				if($scope.editModel.approvalRule.fiveApprovalRole==valeu && sep==5){
					return true;
				}
			}
			return false;
		};
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getApprovalRuleList({
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage,
				requestType:$scope.viewModel.requestType,
				requestRole:$scope.viewModel.requestRole
			},
			function(data){
				$scope.viewModel.dataList = data.approvalRuleBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindData();
	
		//验证
		$scope.viewModel.requiredForm= function() {
			if($scope.editModel.approvalRule.requestType!=14 
					&& $scope.editModel.approvalRule.requestType!=15){
				var type=false;
				if($scope.editForm.requestType.$invalid){
	            	$scope.editForm.requestType.$dirty=true;
	            	type=true;
				}else{
					type=false;
				}
				if($scope.editForm.requestRole.$invalid){
	            	$scope.editForm.requestRole.$dirty=true;
	            	type=true;
				}else{
					type=false;
				}
				//员工类型不等于全员
				if($scope.editModel.approvalRule.requestRole!=99){
					if($scope.editForm.oneApprovalRole.$invalid){
		            	$scope.editForm.oneApprovalRole.$dirty=true;
		            	type=true;
					}else{
						type=false;
					}
				}else{
					if($scope.editForm.lowestApprovalRole.$invalid){
		            	$scope.editForm.lowestApprovalRole.$dirty=true;
		            	type=true;
					}else{
						type=false;
					}
				}
                return type;
			}else{
				var type=false;
				if($scope.editForm.requestType.$invalid){
            		$scope.editForm.requestType.$dirty=true;
            		type=true;
				}else{
					type=false;
				}
				if($scope.editForm.requestRole.$invalid){
            		$scope.editForm.requestRole.$dirty=true;
            		type=true;
				}else{
					type=false;
				}
				//员工类型不等于全员
				if($scope.editModel.approvalRule.requestRole!=99){
					if($scope.editForm.oneApprovalRole.$invalid){
	            		$scope.editForm.oneApprovalRole.$dirty=true;
	            		type=true;
					}else{
						type=false;
					}
				}else{
					if($scope.editForm.lowestApprovalRole.$invalid){
		            	$scope.editForm.lowestApprovalRole.$dirty=true;
		            	type=true;
					}else{
						type=false;
					}
				}
                return type;
			}
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveApprovalRule($scope.editModel.approvalRule,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("新增成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updateApprovalRule($scope.editModel.approvalRule,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}
		};
		
		//删除
		$scope.viewModel.del=function(approvalRule){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteApprovalRule({
					approvalRuleId:approvalRule.approvalRuleId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		
		$scope.viewModel.changeTab = function (index) {
			$scope.viewModel.tab = index;
        };

            //加载审批规则数据
            $scope.viewModel.tab2bindData = function(){
                $scope.viewModel.isLoading = true;
                SalaryApprovalRuleService.getSalaryApprovalRuleList(
                    {"1":  "1"},
                    function(data){
                        $scope.viewModel.dataList2= data;
                        $scope.viewModel.isLoading = false;
                    },function(error) {
                        $scope.viewModel.isLoading = false;
                    });
            };
            $scope.viewModel.tab2bindData();

            //加载
            $scope.viewModel.tab2load=function(){
                $scope.viewModel.show2=true;
            };

            //关闭加载
            $scope.viewModel.tab2unLoad=function(){
                $scope.viewModel.show2=false;
                $scope.editModel2.approvalRule={};
            };

            //新增
            $scope.viewModel.tab2add = function(approval){
                if ( $scope.viewModel.dataList2 && $scope.viewModel.dataList2.length > 0 ) {
                    $rootScope.alertError("审批规则已经存在！");
                    return ;
                }
                $scope.editModel2.title="新增工资审批规则";
                $scope.editModel2.type = true;
                $scope.editModel2.data = {};
                //验证不能为空
                $scope.editForm2.oneEmployeeNo.$dirty=false;
                $scope.viewModel.tab2load();
            };

            //修正
            $scope.viewModel.tab2update=function(rule){
                $scope.editModel2.title="修改工资审批规则";
                $scope.editModel2.type = false;
                $scope.editModel2.data=angular.copy(rule);
                $scope.editModel2.data.salaryRuleId = rule.salaryRuleId;
                $scope.viewModel.tab2load();
            };

            // 新增保存
            $scope.viewModel.tab2save = function () {
                if ( $scope.viewModel.tab2requiredForm() ) {
                    return ;
                }
                $scope.viewModel.isLoading=true;

                var params = $scope.editModel2.data;
                if($scope.editModel2.type){
                    SalaryApprovalRuleService.saveSalaryRule(
                        params,
                        function (data) {
                            $scope.viewModel.tab2bindData();
                            $scope.viewModel.isLoading=false;
                            $rootScope.alertInfo("添加成功。");
                            $scope.viewModel.tab2unLoad();
                        },
                        function (error) {
                            $scope.viewModel.isLoading=false;
                            $rootScope.alertError(error.fault.message);
                        }
                    )
                } else {
                    //params['salaryRuleId'] = $scope.editModel2.data.salaryRuleId;
                    SalaryApprovalRuleService.updateSalaryRule(
                        params,
                        function (data) {
                            $scope.viewModel.tab2bindData();
                            $scope.viewModel.isLoading=false;
                            $rootScope.alertInfo("修改成功。");
                            $scope.viewModel.tab2unLoad();
                        },
                        function (error) {
                            $scope.viewModel.isLoading=false;
                            $rootScope.alertError(error.fault.message);
                        }
                    )
                }
            };

            //删除
            $scope.viewModel.tab2del=function(rule){
                $rootScope.alertConfirm("确定删除吗？",function() {
                    $scope.viewModel.isLoading=true;
                    SalaryApprovalRuleService.deleteSalaryRule({
                        salaryRuleId: rule.salaryRuleId
                    },function(){
                        $scope.viewModel.tab2bindData();
                        $scope.viewModel.isLoading=false;
                        $rootScope.alertInfo("删除成功。");
                        $scope.viewModel.tab2unLoad();
                    },function(error){
                        $scope.viewModel.isLoading=false;
                        $rootScope.alertError(error.fault.message);
                    });
                });
            };

            $scope.viewModel.tab2requiredForm = function () {
                var type = false;
                var data = $scope.editModel2.data;
                if(data.oneEmployeeNo == null){
                    $scope.editForm2.oneEmployeeNo.$dirty=true;
                    $scope.editForm2.oneEmployeeNo.$invalid=true;
                    type=true;
                }else{
                    type=false;
                }
                return type;
            };

            /*   人员选择   */
            $scope.viewEmployeeModel={};
            $scope.viewEmployeeModel.perPage = "10";
            $scope.viewEmployeeModel.rowCount = "1";
            // 人员选择按钮
            $scope.editModel2.chooseUpde = function(typo){
                $scope.viewEmployeeModel.current = typo;
                $scope.viewEmployeeModel.show=true;
                $scope.viewEmployeeModel.title="员工选择";
                $scope.viewEmployeeModel.employeeIsSeparated=null;
                $scope.viewEmployeeModel.regionType=null;
                $scope.viewEmployeeModel.employeeNo=null;
                $scope.viewEmployeeModel.roleName=null;
                $scope.viewEmployeeModel.departmentList=[];
                $scope.viewEmployeeModel.corporationList=[];
                $scope.viewEmployeeModel.employeeList=[];
                $scope.viewEmployeeModel.level=null;
                $(".multiselect-selected-text").text("无");
                //下拉多选框长度
                $scope.multiSelectMaxHeight=300;
                //加载信息
                $scope.viewEmployeeModel.bindSelectRegionTypeData = function(){
                    //查询公司
                    CorporationService.getCorporationList({
                            fromDate:$scope.viewModel.effectiveDate,
                        },
                        function(data){
                            var site = [];
                            $.each(data, function(index, values) {
                                site.push({
                                    label : "("+values.corporationCode+")"+values.corporationName,
                                    value : values.corporationCode
                                });
                            });
                            $('#ectCorporationRule').multiselect('dataprovider',site);
                        },function(error) {

                        });
                };
                $scope.viewEmployeeModel.bindSelectRegionTypeData();


                /*******使用为下拉带勾选控件*********/
                //员工级别
                $('#ectlevelRule').multiselect({
                    includeSelectAllOption: true,
                    enableFiltering: true,
                    delimiterText: '; ',
                    maxHeight: $scope.multiSelectMaxHeight
                });
                //公司
                $('#ectCorporationRule').multiselect({
                    includeSelectAllOption: true,
                    enableFiltering: true,
                    delimiterText: '; ',
                    maxHeight: $scope.multiSelectMaxHeight
                });
                //公司选中事件
                $("#ectCorporationRule").change(function(){
                    var value=$(this).val();
                    var data={};
                    data.corporationCodeList=value;
                    data.fromDate = $scope.viewModel.effectiveDate;
                    if(value==null){
                        var site = [];
                        $('#ectDepartmentNameRule').multiselect('dataprovider',site);
                        $scope.viewEmployeeModel.departmentList=[];
                    }else{
                        //查询部门
                        DepartmentService.getDepartmentListByCorporationCodeList(data,
                            function(data){
                                var site = [];
                                $.each(data, function(index, values) {
                                    site.push({
                                        label : "("+values.departmentCode+")"+values.departmentName,
                                        value : values.departmentCode
                                    });
                                });
                                $('#ectDepartmentNameRule').multiselect('dataprovider',site);
                            },function(error) {

                            });
                    }
                });

                //部门
                $('#ectDepartmentNameRule').multiselect({
                    includeSelectAllOption: true,
                    enableFiltering: true,
                    delimiterText: '; ',
                    maxHeight: $scope.multiSelectMaxHeight
                });
                EmployeeService.getEmployeeList({
                        rowCount : $scope.viewEmployeeModel.perPage,
                        perPage : $scope.viewEmployeeModel.rowCount,
                        fromDate:$scope.viewModel.effectiveDate,
                    },
                    function(data){
                        $scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
                        $scope.viewEmployeeModel.totalData = data.pageTotal;
                        $scope.viewEmployeeModel.isLoading=false;
                    },function(error) {
                        $rootScope.alertError(error.fault.message);
                        $scope.viewEmployeeModel.isLoading=false;
                    });

            }
            //关闭加载
            $scope.viewEmployeeModel.unLoad=function(){
                $scope.viewEmployeeModel.show=false;
                $scope.editModel2.title="新增分配";
            };
            //是否选中
            $scope.viewEmployeeModel.isInAssignmentArr=function(employee){
                // if($scope.editModel2.employeeCorporation.employeeId == employee.employeeId){
                //     return true;
                // }
                return false;
            };
            //加载数据
            $scope.viewEmployeeModel.bindData = function(){
                $scope.viewEmployeeModel.employeeList=[];
                if($scope.viewEmployeeModel.employeeNo!=null){
                    var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
                    for(var i=0;i<employeeNoData.length;i++){
                        $scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
                    }
                }
                $scope.viewModel.isLoading=true;
                //获取基本信息
                EmployeeService.getEmployeeList({
                        employeeList:$scope.viewEmployeeModel.employeeList,
                        departmentList:$scope.viewEmployeeModel.departmentList,
                        corporationList:$scope.viewEmployeeModel.corporationList,
                        employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
                        employeeIsSeparated:"0",
                        perPage:$scope.viewEmployeeModel.rowCount,
                        rowCount:$scope.viewEmployeeModel.perPage,
                        fromDate:$scope.viewModel.effectiveDate,
                    },
                    function(data){
                        $scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
                        $scope.viewEmployeeModel.totalData = data.pageTotal;
                        $scope.viewModel.isLoading=false;
                    },function(error) {
                        $scope.viewModel.isLoading=false;
                    });
            };
            // 点击选择指定人员按钮
            $scope.viewEmployeeModel.employee = function(employee){
                var typo = $scope.viewEmployeeModel.current;
                $scope.viewModel.show2=true;
                $scope.editModel2.data = $scope.editModel2.data || {};
                // $scope.editModel2.data[typo+'EmployeeId'] = employee.employeeId;
                $scope.editModel2.data[typo+'EmployeeNo'] = employee.employeeNo;
                $scope.editModel2.data[typo+'EmployeeName'] = employee.chineseName;
                $scope.viewEmployeeModel.show=false;
            };
            // 员工清空
            $scope.editModel2.chooseDelete = function(typo) {
                $scope.editModel2.data = $scope.editModel2.data || {};
                var attrs = ['EmployeeNo', 'EmployeeName'];
                attrs.map(function (val) {
                    $scope.editModel2.data[typo + val] = null;
                });
            };
} ]);